<template>
  <el-dialog
    v-model="dialogVisible"
    :title="bmcInfo ? '编辑BMC信息' : '添加BMC信息'"
    width="500px"
    :close-on-click-modal="false"
    @closed="resetForm"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px" status-icon>
      <el-form-item label="IP地址" prop="ip_address">
        <el-input v-model="form.ip_address" placeholder="请输入BMC的IP地址" />
      </el-form-item>
      <el-form-item label="MAC地址" prop="mac_address">
        <el-input v-model="form.mac_address" placeholder="请输入BMC的MAC地址" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入BMC的用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          type="password"
          placeholder="请输入BMC的密码"
          show-password
        />
      </el-form-item>
      <el-form-item label="固件版本" prop="firmware_version">
        <el-input v-model="form.firmware_version" placeholder="请输入BMC的固件版本" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" :loading="submitting" @click="handleSubmit">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from 'vue'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import { assetService } from '@/api/assetService'
import type { BMCInfo } from '@/api/types'

const props = defineProps<{
  serverId: number
  visible: boolean
  bmcInfo?: BMCInfo
}>()

const emit = defineEmits<{
  (e: 'update:visible', value: boolean): void
  (e: 'success'): void
}>()

const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value),
})

const formRef = ref<FormInstance>()
const submitting = ref(false)

const form = reactive({
  ip_address: '',
  mac_address: '',
  username: '',
  password: '',
  firmware_version: '',
})

const rules = {
  ip_address: [
    { required: true, message: '请输入IP地址', trigger: 'blur' },
    { pattern: /^(\d{1,3}\.){3}\d{1,3}$/, message: 'IP地址格式不正确', trigger: 'blur' },
  ],
  mac_address: [{ required: true, message: '请输入MAC地址', trigger: 'blur' }],
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  firmware_version: [{ required: true, message: '请输入固件版本', trigger: 'blur' }],
}

const resetForm = () => {
  if (props.bmcInfo) {
    Object.assign(form, props.bmcInfo)
  } else {
    form.ip_address = ''
    form.mac_address = ''
    form.username = ''
    form.password = ''
    form.firmware_version = ''
  }
  if (formRef.value) {
    formRef.value.resetFields()
  }
}

const handleSubmit = async () => {
  if (!formRef.value) return

  await formRef.value.validate(async (valid) => {
    if (valid) {
      submitting.value = true
      try {
        await assetService.createBMC({
          server_id: props.serverId,
          ...form,
        })
        emit('success')
        dialogVisible.value = false
      } catch (error) {
        console.error('Failed to create BMC:', error)
        ElMessage.error('添加BMC信息失败')
      } finally {
        submitting.value = false
      }
    }
  })
}

// 初始化表单
onMounted(() => {
  resetForm()
})
</script>
